#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>

#include "include/socket.h"
#include "include/servHelper.h"
#include "include/parser.h"
#include "include/acsLogger.h"


fileContBuffStruct fileCBuff;

/**
  Function Name     : make_socket 
  Input Paramaters  : port number
  Output Paramaters : socket description 
  Result            : sock
  Description       : create and bind socket
*/
int make_socket (unsigned int port)
{
  int sock;
  struct sockaddr_in name;
     
  sock = socket (PF_INET, SOCK_STREAM, 0);
  if (sock < 0)
  {
     //perror ("socket");
     logtr("ACS Server:","Can not create socket", ERROR, conferr);
     exit (EXIT_FAILURE);
  }
     
       name.sin_family = AF_INET;
       name.sin_port = htons(port);
       name.sin_addr.s_addr = htonl( INADDR_ANY );
       
       if (bind (sock,(struct sockaddr *)&name, sizeof (name)) < 0)
         {
          // perror ("bind");
           logtr("ACS Server:","Can not bind ", ERROR, conferr);
           exit (EXIT_FAILURE);
         }       
     
 return sock;
}


/** 
  Function Name     : connection 
  Input Paramaters  : socket description
  Output Paramaters : NONE 
  Result            : connect
  Description       : create connection 
 */
void connection( int sock )
{

  
 if(listen (sock, 10) < 0)
  {
    //perror ("listen");
    logtr("ACS Server:","Can not listen socket", ERROR, conferr);
    exit (EXIT_FAILURE);
  }

  FD_ZERO (&active_fd_set);
  FD_SET (sock, &active_fd_set);

//  int status = 3;
 
  while (1)
  { 
    timeout.tv_sec = 10;
    timeout.tv_usec = 0;
  
    read_fd_set = active_fd_set;
    if (select(FD_SETSIZE + 1, &read_fd_set, NULL, NULL,&timeout) < 0)
      {
	//perror ("Select");
	logtr("ACS Server:","Can not select socket", ERROR, conferr);
	exit (EXIT_FAILURE);
      }
    for (i = 0; i <= FD_SETSIZE; ++i)
      if( FD_ISSET( i, &read_fd_set ) )
	{
	  if( i == sock )
	    {
	      
	      size = sizeof (clientname);
		newfd1 = accept (sock,
			    (struct sockaddr *) &clientname,
			    &size);
	      if (newfd1 < 0)
		{
		  //perror ("accept");
		  logtr("ACS Server:","Can not accept socket", ERROR, conferr);
		  exit (EXIT_FAILURE);
		}
		/*
 	      fprintf (stderr,"Server: connect from host %s, port %i\n",
 		      (char*)inet_ntoa(clientname.sin_addr),
 		      ntohs(clientname.sin_port));
           */
               char info[128];
	      sprintf(info,"Server: connect from host %s, port %i\n",
		      (char*)inet_ntoa(clientname.sin_addr),
		      ntohs(clientname.sin_port));
	      trace(info);
	      
	      FD_SET (newfd1, &active_fd_set);
	      
	      memset(fileCBuff[i].fileContBuff,0,sizeof(fileCBuff[i].fileContBuff));
	      out_get_param_value0 = 0;
	      out_get_param_value1 = 0;
	      c_reboot = 0;
	      c_download = 0;
	      noSqlError = 0;
	      
	      }
	    
	    else
	    {
		  send_recive_socket( i );
	    }
	
	   
	}
  } 
}
